
const requsts = require("/util/requst");
let interfaceList = [
  {
    name: '我的订单',
    path: '/page/common/myorder/myorder',
    img:"/image/addres.png"
  },
  {
    name: '我的地址',
    path: '/page/common/myaddress/myaddress',
    img:"/image/order.png"
  }
];



Page({
  data: {
    interfaceList,
    canIUseAuthButton:true,
    authCode:"",
    avatarUrl:"/image/avatarUrl.png",
    nickNames:"",
    serviceCall:"",
    workTimes:"",
  },
  onSearchBarTap() {
    my.navigateTo({
      url: '/page/common/search/search',
    });
  },
  oncall() {
    my.makePhoneCall({ number: '15803926230' });
  },
  callnum(){
    var that=this
      my.request({
        url: requsts.url+"/user/userCenter",
        method: 'POST',
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        dataType: 'json',
        success: function(res) {
          console.log(res)
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
          }
          that.setData({
            serviceCall:res.data.data.serviceCall,
            workTimes:res.data.data.workTimes
          })
        },
        fail: function(res) {

        },
      });
  },
  onLoad(){
    this.callnum()
    this.login()
    var that=this
    my.getStorage({
      key: 'userId',
      success: function(res) {
        console.log(res.data)
        if(res.data!=null){
          if(res.data.avatarUrl==""||res.data.avatarUrl==undefined){
              res.data.avatarUrl="/image/avatarUrl.png"
          }
          that.setData({
            nickNames:res.data.nickNames,
            avatarUrl:res.data.avatarUrl,
            canIUseAuthButton:false,
          })
        }
      },
    });
  },
  login(){
      my.getAuthCode({
        scopes: 'auth_base',
        success: (res) => {
         this.setData({
           authCode:res.authCode,
         })
        },
      });
  },
  loginApi(param){
      var that=this
      my.request({
        url: requsts.url+"/user/login",
        method: 'POST',
        data:param,
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        dataType: 'json',
        success: function(res) {
          if(res.data.code!=0){
              util.toastfn(res.data.msg)
          }
          that.setData({
              nickNames:res.data.data.nickName,
              avatarUrl:res.data.data.avatar,
              canIUseAuthButton:false
            })
          my.setStorage({
            key: 'userId',
            data:{
              userId:res.data.data.userId,
              nickNames:res.data.data.nickName,
              avatarUrl:res.data.data.avatar,
            },
            success: function() {
            }
          });
        },
        fail: function(res) {

        },
      });
  },
  onGetAuthorize() {
      var that=this
        my.getOpenUserInfo({
          fail: (res) => {
          },
          success: (res) => {
            let userInfo = JSON.parse(res.response).response // 以下方的报文格式解析两层 response
            console.log(userInfo)
            // my.alert({
            //     content: JSON.stringify(userInfo) 
            //   });
            if(!userInfo.avatar){
              userInfo.avatar=null
            }
            if(!userInfo.nickName){
              userInfo.nickName=null
            }
            var param={
              authCode:that.data.authCode,
              source:"alipay_wallet&avatar",
              avatar:userInfo.avatar,
              nickName:userInfo.nickName,
              gender:userInfo.gender,
              countryCode:userInfo.countryCode,
              province:userInfo.province,
              city:userInfo.city,
            }
            that.loginApi(param)
          }
        });
    },
    //验证用户，然后跳转
    jumpyan(e){
      var url=e.target.dataset.url
      var that=this
        my.getStorage({
          key: 'userId',
          success: function(res) {
            console.log(res.data)
            if(res.data==null){
                my.showToast({
                  type: 'none',
                  content: '请登录',
                  duration: 1500,
                  success: () => {
                    
                  },
                });
            }else{
                my.navigateTo({ url: url+"?userId="+res.data.userId})
            }
          },
        });
    }
});
